export const addArticleCategoryManager = (editor) => {
    const { Components } = editor;

    // 栏目项组件
    Components.addType('categoryItem', {
        model: {
            defaults: {
                tagName: 'div',
                draggable: '.category-list',
                droppable: false,
                
                attributes: { class: 'category-item' ,
                    'data-gjs-type':"categoryItem"
                },
                components: [
                    {
                        type: 'text',
                        content: '栏目名称',
                        attributes: { class: 'category-name' }
                    },
                    {
                        type: 'button',
                        content: '编辑',
                        attributes: { class: 'category-edit-button' },
                    },
                    {
                        type: 'button',
                        content: '删除',
                        attributes: { class: 'category-delete-button' },
                    }
                ],
                script() {
                    const itemEl = this;

                    // 绑定编辑按钮点击事件
                    itemEl.querySelector('.category-edit-button').addEventListener('click', () => {
                        const newName = prompt('请输入新的栏目名称:', itemEl.querySelector('.category-name').innerText);
                        if (newName) {
                            itemEl.querySelector('.category-name').innerText = newName;
                        }
                    });

                    // 绑定删除按钮点击事件
                    itemEl.querySelector('.category-delete-button').addEventListener('click', () => {
                        itemEl.remove();
                    });
                },
                styles: `
                    .category-item {
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                        padding: 10px 15px;
                        border: 1px solid #ddd;
                        margin-bottom: 10px;
                        background-color: #fff;
                        border-radius: 5px;
                        transition: all 0.3s ease;
                    }
                    .category-item:hover {
                        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                    }
                    .category-name {
                        flex: 1;
                        font-size: 16px;
                    }
                    .category-edit-button{
                        margin-left: 10px;
                        cursor: pointer;
                        background-color: #007bff;
                        color: white;
                        border: none;
                        padding: 5px 10px;
                        border-radius: 5px;
                        transition: background-color 0.3s;
                    }
                    .category-edit-button:hover{
                        background-color: #0056b3;
                    }
                        .category-delete-button {
                        margin-left: 10px;
                        cursor: pointer;
                         background-color: #dc3545;
                        color: white;
                        border: none;
                        padding: 5px 10px;
                        border-radius: 5px;
                        transition: background-color 0.3s;
                    }
                        .category-delete-button:hover {
                        background-color: rgb(219 119 128);
                    }


                `,
            }
        },
      
    });

    // 栏目管理组件
    Components.addType('categoryManager', {
        model: {
            defaults: {
                tagName: 'div',
                attributes: { class: 'category-manager' },
                resizable: true, 
                components: [
                    {
                        type: 'text',
                        content: '栏目管理',
                        attributes: { class: 'category-manager-title' }
                    },
                   
                    {
                        type: 'div',
                        attributes: { class: 'category-list' },
                        components: [
                            {
                                type: 'categoryItem',
                               
                            },
                            {
                                type: 'categoryItem',
                              
                            }
                        ]
                    }
                ],
                script() {
                    

                    
                },
                styles: `
                    .category-manager {
                        padding: 20px;
                        border: 1px solid #ddd;
                        background-color: #f9f9f9;
                        border-radius: 8px;
                        width: 80%;
                        margin: 0 auto;
                    }
                    .category-manager-title {
                        font-size: 24px;
                        margin-bottom: 20px;
                        text-align: center;
                        font-weight: bold;
                    }
                    
                    .category-list {
                        display: flex;
                        flex-direction: column;
                        gap: 10px;
                    }
                    .category-item {
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                        padding: 10px 15px;
                        border: 1px solid #ddd;
                        margin-bottom: 10px;
                        background-color: #fff;
                        border-radius: 5px;
                        transition: all 0.3s ease;
                    }
                    .category-item:hover {
                        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                    }
                    .category-name {
                        flex: 1;
                        font-size: 16px;
                    }
                    
                `,
            }
        }
    });

    // 注册栏目管理组件
    editor.BlockManager.add('category-manager', {
        label: '栏目管理组件',
        category: '栏目类',
        content: { type: 'categoryManager' },
        media:`<svg xmlns="http://www.w3.org/2000/svg" height="30" width="30" viewBox="0 -960 960 960" width="24px" fill="#5f6368"><path d="M200-120q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h560q33 0 56.5 23.5T840-760v560q0 33-23.5 56.5T760-120H200Zm0-507h560v-133H200v133Zm0 214h560v-134H200v134Zm0 213h560v-133H200v133Zm40-454v-80h80v80h-80Zm0 214v-80h80v80h-80Zm0 214v-80h80v80h-80Z"/></svg>`
    });
};
